package com.wz.utils.transaction.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author : Wang Zhen.
 * @Date : Created in 17:01 2022/12/20
 * @Description :
 * @Modified By   :
 * @Version :
 */
public class JDBCTest {
	public static void main(String[] args) throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			// 加载驱动程序，并连接数据库
			conn = DBConn.conn();
			String sql = "INSERT INTO test.user(name,age,sex) VALUES(?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, "老张");
			ps.setInt(2, 68);
			ps.setString(3, "男");

			DBConn.startTransaction();
			ps.execute();
			DBConn.commit();
			System.out.println("插入成功！");
		} catch (SQLException e) {
			DBConn.rollback();
			e.printStackTrace();
		} finally {
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
			DBConn.close();
		}

	}
}
